package com.enation.app.javashop.api.client;

import com.enation.app.javashop.framework.util.BeanUtil;
import com.enation.app.javashop.framework.util.JsonUtil;
import com.enation.app.javashop.model.promotion.fulldiscount.dos.FullDiscountGiftDO;
import com.enation.app.javashop.model.security.ScanModuleDTO;
import com.enation.app.javashop.model.security.ScanResult;
import com.enation.app.javashop.model.trade.cart.dos.OrderPermission;
import com.enation.app.javashop.model.trade.order.dos.OrderDO;
import com.enation.app.javashop.model.trade.order.dos.OrderItemsDO;
import com.enation.app.javashop.model.trade.order.dto.OrderDetailDTO;
import com.enation.app.javashop.model.trade.order.dto.OrderSkuDTO;
import com.enation.app.javashop.model.trade.order.enums.CommentStatusEnum;
import com.enation.app.javashop.model.trade.order.enums.OrderMetaKeyEnum;
import com.enation.app.javashop.model.trade.order.enums.OrderStatusEnum;
import com.enation.app.javashop.model.trade.order.vo.DeliveryVO;
import com.enation.app.javashop.model.trade.order.vo.OrderDetailVO;
import com.enation.app.javashop.model.trade.order.vo.OrderSkuVO;
import com.enation.app.javashop.model.trade.order.vo.OrderStatusNumVO;
import com.enation.app.javashop.service.trade.order.OrderMetaManager;
import com.enation.app.javashop.service.trade.order.OrderOperateManager;
import com.enation.app.javashop.service.trade.order.OrderQueryManager;
import io.swagger.annotations.Api;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 订单相关API
 *
 * @author Snow create in 2018/8/6
 * @version v2.0
 * @since v7.0.0
 */
@Api(description = "订单相关API")
@RestController
@RequestMapping("/client/orders")
@Validated
@ApiIgnore
public class OrderClientController {


    @Autowired
    private OrderQueryManager orderQueryManager;

    @Autowired
    private OrderOperateManager orderOperateManager;

    @Autowired
    private OrderMetaManager orderMetaManager;

    @GetMapping(value = "/{order_sn}/detail")
    public OrderDetailVO getOrderDetail(@ApiIgnore @PathVariable(value = "order_sn") String orderSn) {
        return orderQueryManager.getModel(orderSn, null);
    }


    @GetMapping(value = "/{order_sn}")
    public OrderDetailDTO getModel(@ApiIgnore @PathVariable(value = "order_sn") String orderSn) {
        OrderDetailVO orderDetailVO = this.orderQueryManager.getModel(orderSn, null);
        OrderDetailDTO detailDTO = new OrderDetailDTO();
        BeanUtils.copyProperties(orderDetailVO, detailDTO);
        detailDTO.setOrderSkuList(new ArrayList<>());

        for (OrderSkuVO skuVO : orderDetailVO.getOrderSkuList()) {
            OrderSkuDTO skuDTO = new OrderSkuDTO();
            BeanUtil.copyProperties(skuVO, skuDTO);
            detailDTO.getOrderSkuList().add(skuDTO);
        }

        String json = this.orderMetaManager.getMetaValue(detailDTO.getSn(), OrderMetaKeyEnum.GIFT);
        List<FullDiscountGiftDO> giftList = JsonUtil.jsonToList(json, FullDiscountGiftDO.class);
        detailDTO.setGiftList(giftList);

        return detailDTO;
    }

    @PostMapping(value = "/{order_sn}/comment-status")
    public void updateOrderCommentStatus(@PathVariable("order_sn") String orderSn, @RequestParam("status_enum") String statusEnum) {
        CommentStatusEnum commentStatusEnum = CommentStatusEnum.valueOf(statusEnum);
        this.orderOperateManager.updateCommentStatus(orderSn, commentStatusEnum);
    }


    @PostMapping(value = "/{order_sn}/order-sku-service-status")
    public void updateOrderItemServiceStatus(@PathVariable("order_sn") String sn, @RequestBody List<OrderSkuDTO> orderSkuDTOList) {
        OrderDetailVO orderDetailVO = this.orderQueryManager.getModel(sn, null);
        List<OrderSkuVO> orderSkuVOList = orderDetailVO.getOrderSkuList();
        for (OrderSkuVO orderSkuVO : orderSkuVOList) {
            for (OrderSkuDTO orderSkuDTO : orderSkuDTOList) {
                if (orderSkuVO.getSkuId().equals(orderSkuDTO.getSkuId())) {
                    orderSkuVO.setServiceStatus(orderSkuDTO.getServiceStatus());
                }
            }
        }
        this.orderOperateManager.updateItemJson(JsonUtil.objectToJson(orderSkuVOList), sn);
    }

    @GetMapping(value = "/{trade_sn}/order-sku-list")
    public List<OrderDetailDTO> getOrderByTradeSn(@PathVariable("trade_sn") String tradeSn) {
        List<OrderDetailDTO> orderDetailDTOList = this.orderQueryManager.getOrderByTradeSn(tradeSn);
        return orderDetailDTOList;
    }


    @PostMapping(value = "/{order_sn}/pay")
    public void payOrder(@PathVariable("order_sn") String sn, @RequestParam("price") Double price, @RequestParam("return_trade_no") String returnTradeNo, @RequestParam("permission") String permission) {
        OrderPermission orderPermission = OrderPermission.valueOf(permission);
        this.orderOperateManager.payOrder(sn, price, returnTradeNo, orderPermission);
    }


    @GetMapping(value = "/{member_id}/order-num")
    public Integer getOrderNumByMemberID(@PathVariable("member_id") Long memberId) {
        Integer num = this.orderQueryManager.getOrderNumByMemberId(memberId);
        return num;
    }


    @GetMapping(value = "/{member_id}/order-comment-num")
    public Integer getOrderCommentNumByMemberID(@PathVariable("member_id") Long memberId, @RequestParam("comment_status") String commentStatus) {
        return this.orderQueryManager.getOrderCommentNumByMemberId(memberId, commentStatus);
    }

    @GetMapping("/order-status-num")
    public OrderStatusNumVO getOrderStatusNum(@RequestParam(value = "member_id", required = false) Long memberId, @RequestParam(value = "seller_id", required = false) Long sellerId) {

        return this.orderQueryManager.getOrderStatusNum(memberId, sellerId);
    }

    @PostMapping("/{sn}/order-status")
    public boolean updateOrderStatus(@PathVariable("sn") String sn, @RequestParam("order-status") OrderStatusEnum orderStatus) {
        orderOperateManager.updateOrderStatus(sn, orderStatus);
        return true;
    }

    @PostMapping("/{sn}/trade-status")
    public boolean updateTradeStatus(@PathVariable("sn") String sn, @RequestParam("order-status") OrderStatusEnum orderStatus) {
        orderOperateManager.updateTradeStatus(sn, orderStatus);
        return true;
    }

    @PostMapping("/item-refund-price")
    public void addOrderItemRefundPrice(@RequestBody OrderDO orderDO){

        orderOperateManager.updateItemRefundPrice(this.orderQueryManager.getModel(orderDO.getSn(), null));
    }

    @GetMapping("/order-items")
    public List<OrderItemsDO> orderItems(@RequestParam("order_sn")String orderSn){

        return orderQueryManager.orderItems(orderSn);
    }

    @PostMapping(value = "/update-comment-status")
    public void updateItemsCommentStatus(@RequestParam("order_sn") String orderSn, @RequestParam("goods_id")Long goodsId, @RequestParam("comment_status")CommentStatusEnum commentStatus){
        orderOperateManager.updateItemsCommentStatus(orderSn, goodsId, commentStatus);
    }

    @GetMapping(value = "/{order_sn}/orderdo")
    public OrderDO getOrder(@PathVariable("order_sn") String orderSn){

        return this.orderQueryManager.getOrder(orderSn);
    }

    @RequestMapping(value = "/ship", method = RequestMethod.POST)
    public void ship(@RequestBody DeliveryVO deliveryVO, @RequestParam("permission") OrderPermission permission){

        orderOperateManager.ship(deliveryVO, OrderPermission.client);
    }

    @GetMapping("/by-goods/{goods_id}")
    public List<OrderDO> listOrderByGoods(@PathVariable("goods_id") Long goodsId, @RequestParam("member_id") Long memberId,@RequestParam("month") Integer month) {
        return orderQueryManager.listOrderByGoods(goodsId, memberId, month);
    }

    @GetMapping("/promotion-typeand-num")
    public List<Map> getItemsPromotionTypeandNum(@RequestParam("order_sn") String  orderSn){

        return orderQueryManager.getItemsPromotionTypeandNum(orderSn);
    }

    @GetMapping("/by-sns")
    public List<OrderDO> queryOrderList(@RequestBody List<String> orderSnList) {
        return orderQueryManager.queryOrderList(orderSnList);
    }

    @PostMapping("/scan")
    public ScanResult scanModule(@RequestBody ScanModuleDTO scanModuleDTO){
        return orderQueryManager.scanModule(scanModuleDTO);
    }

    @GetMapping("/resign")
    public void reSign(){
        orderQueryManager.reSign();
    }

    @GetMapping("/repair/{order_id}")
    public void repair(@PathVariable("order_id")Long orderId){
        orderQueryManager.repair(orderId);
    }
}
